package com.cms.dao.impl;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.hibernate.NonUniqueObjectException;
import org.hibernate.criterion.DetachedCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateSystemException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cms.common.PageResult;
import com.cms.dao.BaseDao;

public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{
	private Class<T> entityClass;
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BaseDaoImpl() {
		this.entityClass = null;
        Class c = getClass();
        Type type = c.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
            this.entityClass = (Class<T>) parameterizedType[0];
        }
	}

	@Override
	public void save(T... entities) {
		for (T entity : entities) {
			this.save(entity);
		}
	}

	@Override
	public void save(T entity) {
		getHibernateTemplate().save(entity);  
	}

	@Override
	public void saveOrUpdate(T... entities) {
		for (T entity : entities) {
		 getHibernateTemplate().update(entity); 
		}
	}

	@Override
	public void update(T... entities) {
		for (T entity : entities) {
			 getHibernateTemplate().update(entity);
		}
	}

	@Override
	public void update(Collection<T> entities) {
		for (T entity : entities) {
			try {
				 getHibernateTemplate().update(entity);
			} catch (NonUniqueObjectException nouEx) {
				 getHibernateTemplate().merge(entity);
			} catch (HibernateSystemException hsEx) {
				 getHibernateTemplate().merge(entity);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public T get(Serializable id) {
		return (T) getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public void delete(T... entities) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Collection<T> entities) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> list(Class<T> clazz) {
		return getHibernateTemplate().loadAll(entityClass); 
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public PageResult page(PageResult pageArg, String... orderArg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResult page(int pageNow, int pageSize, DetachedCriteria criteria) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResult page(int pageNo, int pageSize, String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> query(DetachedCriteria criteria) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int rowCount(String sql, Object... args) {
		// TODO Auto-generated method stub
		return 0;
	}

}
